package com.example.llm.learn.api.controller;

import com.example.llm.learn.api.dto.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@CrossOrigin(origins = {"http://localhost:5173", "http://127.0.0.1:5500"})
@RestController
public class AttrController implements InitializingBean {
    private List<AttrDTO> attrList = new LinkedList<>();
    private List<AttrNameDTO> attrNameValueList = new LinkedList<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        attrList.add(new AttrDTO().setId(1L).setName("数码").setParentId(0L));
        attrList.add(new AttrDTO().setId(11L).setName("手机").setParentId(1L));
        attrList.add(new AttrDTO().setId(12L).setName("电脑").setParentId(1L));
        attrList.add(new AttrDTO().setId(13L).setName("相机").setParentId(1L));
        attrList.add(new AttrDTO().setId(14L).setName("录音笔").setParentId(1L));
        attrList.add(new AttrDTO().setId(111L).setName("智能机").setParentId(11L));
        attrList.add(new AttrDTO().setId(113L).setName("轰炸机").setParentId(11L));
        attrList.add(new AttrDTO().setId(121L).setName("笔记本").setParentId(12L));
        attrList.add(new AttrDTO().setId(122L).setName("台式机").setParentId(12L));
        attrList.add(new AttrDTO().setId(131L).setName("卡片机").setParentId(13L));
        attrList.add(new AttrDTO().setId(2L).setName("家居").setParentId(0L));
        attrList.add(new AttrDTO().setId(21L).setName("桌子").setParentId(2L));
        attrList.add(new AttrDTO().setId(211L).setName("木桌").setParentId(21L));

        List<AttrValueDTO> valueList1 = new LinkedList<>();
        valueList1.add(new AttrValueDTO().setId(11111L).setName("安卓").setAttrNameId(1111L));
        valueList1.add(new AttrValueDTO().setId(11112L).setName("苹果").setAttrNameId(1111L));
        attrNameValueList.add(new AttrNameDTO().setId(1111L).setName("系统").setAttrId(111L).setValueList(valueList1));
        List<AttrValueDTO> valueList2 = new LinkedList<>();
        valueList2.add(new AttrValueDTO().setId(11121L).setName("64G").setAttrNameId(1112L));
        valueList2.add(new AttrValueDTO().setId(11122L).setName("128G").setAttrNameId(1112L));
        valueList2.add(new AttrValueDTO().setId(11123L).setName("256G").setAttrNameId(1112L));
        attrNameValueList.add(new AttrNameDTO().setId(1112L).setName("内存").setAttrId(111L).setValueList(valueList2));
        List<AttrValueDTO> valueList6 = new LinkedList<>();
        valueList6.add(new AttrValueDTO().setId(11131L).setName("红色").setAttrNameId(1113L));
        valueList6.add(new AttrValueDTO().setId(11132L).setName("蓝色").setAttrNameId(1113L));
        valueList6.add(new AttrValueDTO().setId(11133L).setName("绿色").setAttrNameId(1113L));
        attrNameValueList.add(new AttrNameDTO().setId(1113L).setName("颜色").setAttrId(111L).setValueList(valueList6));
        List<AttrValueDTO> valueList3 = new LinkedList<>();
        valueList3.add(new AttrValueDTO().setId(11311L).setName("美国").setAttrNameId(1131L));
        valueList3.add(new AttrValueDTO().setId(11312L).setName("中国").setAttrNameId(1131L));
        attrNameValueList.add(new AttrNameDTO().setId(1131L).setName("产地").setAttrId(113L).setValueList(valueList3));
        List<AttrValueDTO> valueList4 = new LinkedList<>();
        valueList4.add(new AttrValueDTO().setId(21111L).setName("圆形").setAttrNameId(2111L));
        valueList4.add(new AttrValueDTO().setId(21111L).setName("长方形").setAttrNameId(2111L));
        attrNameValueList.add(new AttrNameDTO().setId(2111L).setName("形状").setAttrId(211L).setValueList(valueList4));
    }

    @GetMapping(value = "/attr/list")
    public CommonResponse<List<AttrDTO>> list(@RequestParam Long parentId) {
        return CommonResponse.ok(findAttrByParentId(parentId));
    }

    private List<AttrDTO> findAttrByParentId(Long parentId) {
        List<AttrDTO> attrDTOList = new LinkedList<>();
        Iterator<AttrDTO> iterator = attrList.iterator();
        while (iterator.hasNext()) {
            AttrDTO attrDTO = iterator.next();
            if (attrDTO.getParentId().equals(parentId)) {
                attrDTOList.add(attrDTO);
            }
        }
        return attrDTOList;
    }

    @GetMapping(value = "/attrName/list")
    public CommonResponse<List<AttrNameDTO>> attrNameList(@RequestParam Long attrId) {
        return CommonResponse.ok(findById(attrId));
    }

    private List<AttrNameDTO> findById(Long attrId) {
        List<AttrNameDTO> attrNameDTOS = new LinkedList<>();
        Iterator<AttrNameDTO> iterator = attrNameValueList.iterator();
        while (iterator.hasNext()) {
            AttrNameDTO attrNameDTO = iterator.next();
            if (attrNameDTO.getAttrId().equals(attrId)) {
                attrNameDTOS.add(attrNameDTO);
            }
        }
        return attrNameDTOS;
    }

    @PostMapping(value = "/attrName/add")
    public CommonResponse<Long> attrNameList(@RequestBody AttrNameDTO attrNameDTO) {
        if (Objects.isNull(attrNameDTO.getId())) {
            attrNameDTO.setId(System.currentTimeMillis());
            attrNameDTO.getValueList().forEach(item -> {
                item.setId(System.nanoTime());
                item.setAttrNameId(attrNameDTO.getId());
            });
            attrNameValueList.add(attrNameDTO);
            return CommonResponse.ok(attrNameDTO.getId());
        } else {
            Iterator<AttrNameDTO> iterator = attrNameValueList.iterator();
            while (iterator.hasNext()) {
                AttrNameDTO next = iterator.next();
                if (next.getId().equals(attrNameDTO.getId())) {
                    next.setName(attrNameDTO.getName());
                    next.setValueList(attrNameDTO.getValueList());
                    next.getValueList().forEach(item -> {
                        if (Objects.isNull(item.getId())) {
                            item.setId(System.nanoTime());
                        }
                        item.setAttrNameId(attrNameDTO.getId());
                    });
                    break;
                }
            }
            return CommonResponse.ok(attrNameDTO.getId());
        }
    }

    @DeleteMapping(value = "/attrName/delete")
    public CommonResponse<Long> delete(@RequestParam Long attrNameId) {
        attrNameValueList = attrNameValueList.stream().filter(item -> !item.getId().equals(attrNameId)).collect(Collectors.toList());
        return CommonResponse.ok(attrNameId);
    }
}
